import router from "./router.js";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import store from "../store";
NProgress.configure({
  ease: "ease",
  speed: 500,
});

const writeNames = ["/login"];
router.beforeEach((to, from, next) => {
  NProgress.start();
  const token = sessionStorage.getItem("token");
  console.log("路由守卫：当前路径", to.path);
  console.log("路由守卫：是否有token", !!token); // 登录成功后应显示 true

  if (token) {
    console.log("路由守卫：有token，准备放行到", to.path);
    if (to.path === "/login") {
      next("/lstm");
    } else {
      next(); // 关键：确认是否执行到这里
    }
  } else {
    console.log("路由守卫：无token，跳转登录页");
    if (writeNames.includes(to.path)) {
      next();
    } else {
      next("/login");
    }
  }
});

router.afterEach((transition) => {
  NProgress.done();
});

export default router;
